package com.paddyi.controller;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.paddyi.commons.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.paddyi.commons.http.HttpResult;
import com.paddyi.commons.page.MyPage;
import com.paddyi.model.SsoUser;
import com.paddyi.service.ISsoUserService;

/**
 *  (SsoUser)
 * @author paddy
 * @since  2019-07-28 05:01:19
 */
@RestController
@RequestMapping("ssoUser")
public class SsoUserController {

	@Autowired
	private ISsoUserService ssoUserService;

	/**
	 * 保存
	 * @param record
	 * @return
	 */	
	@PostMapping(value="/register")
	@ResponseBody
	public HttpResult add( SsoUser record) {
		record.setCreateTime(new Date());
		if(StringUtils.isBlank(record.getImg())){
			record.setImg("http://qiniu.paddyi.com/1560309450637.jpg?imageView2/1/w/200/h/200/q/75|imageslim");
		}
		System.out.println(record);
		return  HttpResult.ok("ok");
//		return HttpResult.ok(ssoUserService.save(record));
	}

    /**
     * 删除
     * @param records
     * @return
     */
	@PostMapping(value="/del")
	public HttpResult del(@RequestBody List<Long> records) {
		return HttpResult.ok(ssoUserService.removeByIds(records));
	}

	/**
     * 修改
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody SsoUser record) {
        return HttpResult.ok(ssoUserService.updateById(record));
    }

    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     * @param page
     * @return
     */    
	@PostMapping(value="/findPage")
	public HttpResult findPage(@RequestBody MyPage<SsoUser> page) {
	    if(page.getQuerycondition()!=null){
            SsoUser ssoUser = page.getQuerycondition().get(0);
            QueryWrapper<SsoUser> queryWrapper = new QueryWrapper<>();
            for(Field f:ssoUser.getClass().getDeclaredFields()){
                f.setAccessible(true);
                try {
                    if(f.get(ssoUser) != null && !"serialVersionUID".equals(f.getName())){
                        queryWrapper.eq(f.getName(), f.get(ssoUser));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            return HttpResult.ok(ssoUserService.page(page,queryWrapper));
        }
		return HttpResult.ok(ssoUserService.page(page));
	}
	
    /**
     * 根据主键查询
     * @param id
     * @return
     */ 	
	@GetMapping(value="/find")
	public HttpResult find(@RequestParam Long id) {
		return HttpResult.ok(ssoUserService.getById(id));
	}
}
